package com.javaee.xiangyang.controller.Android;


import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.UserComment;
import com.javaee.xiangyang.entity.Comment;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.service.AccountService;
import com.javaee.xiangyang.service.CommentService;
import com.javaee.xiangyang.vo.ServerResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/Comment")
public class CommentController {

    @Resource
    CommentService commentService;

    @Resource
    AccountService accountService;

    /**
     * 用户生成评论
     */
    @PostMapping("/AddComment")
    public ServerResponse<Comment> addComment(Comment comment) {
        ServiceResultEnum serviceResultEnum = commentService.addComment(comment);
        if (serviceResultEnum.equals(ServiceResultEnum.SUCCESS)) {
            return ServerResponse.creatServerResponseBySuccess();
        }
        return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
    }

    /**
     * 用户在手机上查看自己所有的评论
     */
    @GetMapping("/GetAllCommentByUserAccount")
    public ServerResponse<List<UserComment>> getAllCommentByUserAccount(User user) {
        User user1 = accountService.GetUser(user);
        if (user1 != null) {
            //用户存在
            return ServerResponse.creatServerResponseBySuccess(commentService.findAllCommentByUserAccount(user));
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }

    /**
     * 用户删除自己评论
     */
    @PostMapping("/DeleteByCommentId")
    public ServerResponse<Comment> deleteByCommentId(Comment comment) {
        if (accountService.GetUser(new User(comment.getCommentUserAccount())) != null) {
            //用户存在
            ServiceResultEnum serviceResultEnum = commentService.deleteCommentByCommentId(comment);
            if (serviceResultEnum.equals(ServiceResultEnum.SUCCESS)) {
                return ServerResponse.creatServerResponseBySuccess();
            }
            return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }
}
